<script src="{{ getAssets }}js/amazeui.datatables.min.js?v={{ assets_version }}"></script>
<script>
	var tables = $('#dataTables').DataTable({
		"sAjaxSource" : "{{ urlfor "OutpatientController.PostInspect"}}"
		,"bLengthChange": false
		,"fnServerData" : function(sSource, aoData, fnCallback) {
			var params = checkQueryTableData(aoData)
			params.ArchivesId = {{.m.Id}};
			
			$.post(sSource, JSON.stringify(params),function(resp) {
				fnCallback(resp);
			},'json')
		}
		,columns: [
			{"mDataProp" : "Id", "bSortable": false}
			
			,{"mDataProp" : "ProductName"}
			,{"mDataProp": "CreatedBy", "bSortable": false}
			,{"mDataProp": "CreatedAt", render:function(data, type, full, meta) {
				return timetransDate(data);	
			}}
			,{"mDataProp": "PayStatus", render:function(data, type, full, meta) {
				switch(data){
					case 2:
						return '<span class="am-badge am-badge-danger">待确费</span>';	
					case 1:
						return '<span class="am-badge am-badge-secondary">已确费</span>';	
				}
			}} 
			,{   
				"mDataProp": "Action", 
				"bSortable": false, 
				"sWidth":"6%",
				"render": function ( data, type, full, meta ) { 
					var btns = '';
					if(full.PayStatus == 2){
						btns = '<a href="javascript:;" class="am-btn am-btn-success am-btn-xs" onclick="deleteInspect('+ full.Id +')">删除</a>';
					}
					return btns;  
				}  
			 } 
		]
		,"aLengthMenu":[20, 50, 100, 200]
		,'bLengthChange': true
		,"serverSide": true
		,"bPaginate" : true
		,"bFilter" : false	//是否启动过滤、搜索功能 
		,"bInfo" : true //是否显示页脚信息，DataTables插件左下角显示记录数
		,"bProcessing" : true //DataTables载入数据时，是否显示‘进度’提示  
		  
	});
	
	$("#searchsubmit").bind("click", function () { //点击按钮 触发table重新请求服务器
		reloadDataTable();
	});
	function reloadDataTable() {
		$("#dataTables").dataTable().fnDraw(false);
	}
	
	function selectProducts() {
        var url = '{{ urlfor "ProductsController.SelectProducts"}}?ty=t60';
        layer.open({
            type: 2
            ,title: '选择检查项目'
            ,shadeClose: false
            ,shade: 0.2
            ,maxmin: true
            ,shift: 1
            ,area: ['1000px', '600px']
			,zIndex: layer.zIndex //重点1
            ,content: url
			,success: function(layero){
			    layer.setTop(layero); //重点2
			}
        });
		return false;
	}	
	$('#addInspect').click(function(){
		selectProducts();
		return false;
	});
	
	
	//保存选择的项目
	function receiveFormData(data) {
		if(data.Id<=0) {
			layer.msg('请选择检查项目');
			return false;
		}
		var archives_id = $('#archives_id').val(); //档案ID
		var products_id = data.Id; //产品ID
		var product_name = data.FullName;
		var amount = 1; 
		var unit_price = data.SuggestedPrice;
		$.post('{{ urlfor "OutpatientController.SaveInspect"}}', {'archives_id':archives_id,'products_id':products_id,'product_name':product_name, 'amount':amount, 'unit_price':unit_price} , function(j){
			if(j.code == 1) {
				layer.msg(j.msg);
				return false;	
			}
			reloadDataTable();
			layer.msg('检查项目添加成功');
		}, 'json');
		return false;
	}
	
	//删除项目
	function deleteInspect(id) {
		if(id<=0) {
			layer.msg('请选择检查项目');
			return false;
		}
		$.post('{{ urlfor "OutpatientController.DeleteInspect"}}', {'id':id} , function(j){
			if(j.code == 1) {
				layer.msg(j.msg);
				return false;	
			}
			reloadDataTable();
		}, 'json');
	}
	
</script>